package pro1.t3;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author xingyu Xue
 * @Date 2024/1/30 11:40
 * 无重复字符的最长子串
 */
public class t3 {
    /**
     * 我自己写的算法，
     * "abcabcbb"3
     * "bbbbb" 1
     * "pwwkew" 3
     * 前两个测试用例ok第三个是2，算法还不行，后面改改改不回去了，总体思想不太对
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();

        int MaxReturn = 0;
        //先拿0位和后面每一位较比是否相同
        for (int i = 0; i < chars.length; i++) {
            for (int j = i+1; j < chars.length; j++) {
                if (chars[i]==chars[j]) {
                    if (j-i > MaxReturn) {
                        MaxReturn = j-i;
                    }
                }
            }
            if (i<=chars.length-3){
                if (chars[i+1] == chars[i+2]) {
                    if (i+2 > MaxReturn) MaxReturn = i+2;
                }

            }else if (chars[i]==chars[i+1]){//i=4
                if (i > MaxReturn) MaxReturn = i+1;
            }

        }
        return MaxReturn;
    }

    /**
     * 正解，滑动窗口
     * @param
     * @return
     */
//    public int lengthOfLongestSubstring2(String s){
//        char[] chars = s.toCharArray();
//        int max=0;
//        int left = 0;
//        Map<Character,Integer> map =new HashMap<Character, Integer>();
//        for (int i = 0; i < chars.length; i++) {
//            if (map.containsKey(chars[i])){
////                map.r
//
//            }else {
//                map.put(chars[i],i);
//            }
//        }
//
//    }
    public static void main(String[] args) {
        t3 t3 = new t3();
        System.out.println(t3.lengthOfLongestSubstring("bbbbb"));//1
        System.out.println(t3.lengthOfLongestSubstring("abcabcbb")); //3
        System.out.println(t3.lengthOfLongestSubstring("pwwkew"));//3
    }
}
